<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /*  var hasPathSum = function (root, targetSum) {
                let res = []
                function preOrder(root, path, sum) {
                    if (!root) return
                    sum += root.val
                    path.push(root.val)
                    if (!root.left && !root.right && sum == targetSum) {
                        return (res = [...path])
                    }
                    if (root.left) {
                        preOrder(root.left, path, sum)
                        path.pop()
                    }
                    if (root.right) {
                        preOrder(root.right, sum)
                        path.pop()
                    }
                }
                return flag
            }
            hasPathSum() */
            let tree = {
                val: 1,
                left: {
                    val: 2,
                    left: {
                        val: 4,
                        left: {
                            val: 8,
                        },
                    },
                    right: {
                        val: 5,
                    },
                },
                right: {
                    val: 3,
                    left: {
                        val: 6,
                    },
                    right: {
                        val: 7,
                        left: {
                            val: 9,
                        },
                    },
                },
            }
            /* 遍历一条边 */
            var pathSum = function (root, targetSum) {
                if (!root) return false
                //注意，头节点必须带着
                return preOrder(root, root.val)
                function preOrder(cur, sum) {
                    //找到了一条边
                    if (!cur.left && !cur.right && sum == targetSum) {
                        return true
                    }
                    if (!cur.left && !cur.right) {
                        return false //该边结束，没找到
                    }
                    //确定循环条件
                    if (cur.left) {
                        sum += cur.left.val
                        if (preOrder(cur.left, sum)) return true
                        sum -= cur.left.val
                    }
                    if (cur.right) {
                        sum += cur.right.val
                        if (preOrder(cur.right, sum)) return true
                        sum -= cur.right.val
                    }
                    //所有边遍历完毕 可以选择return false，如果不return false就是undefined，说明没有这条路径，也可以后面判断
                    return false
                }
            }
            console.log(pathSum(tree, 7))
        </script>
    </body>
</html>
